home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JDCOLOR.C < prev    next >
C/C++ Source or Header  |  1991-12-01  |  6KB  |  195 lines

  1. /*
  2.  * jdcolor.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains output colorspace conversion routines.
  9.  * These routines are invoked via the methods color_convert
  10.  * and colorout_init/term.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. /*
  17.  * Initialize for colorspace conversion.
  18.  */
  19.  
  20. METHODDEF void
  21. colorout_init (decompress_info_ptr cinfo)
  22. {
  23.   /* no work needed */
  24. }
  25.  
  26.  
  27. /*
  28.  * Convert some rows of samples to the output colorspace.
  29.  * This version handles YCbCr -> RGB conversion.
  30.  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  31.  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  32.  */
  33.  
  34. METHODDEF void
  35. ycc_rgb_convert (decompress_info_ptr cinfo, int num_rows,
  36.          JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  37. {
  38.   register INT32 y, u, v, x;
  39.   register JSAMPROW inptr0, inptr1, inptr2;
  40.   register JSAMPROW outptr0, outptr1, outptr2;
  41.   register long col;
  42.   register long width = cinfo->image_width;
  43.   register int row;
  44.   
  45.   for (row = 0; row < num_rows; row++) {
  46.     inptr0 = input_data[0][row];
  47.     inptr1 = input_data[1][row];
  48.     inptr2 = input_data[2][row];
  49.     outptr0 = output_data[0][row];
  50.     outptr1 = output_data[1][row];
  51.     outptr2 = output_data[2][row];
  52.     for (col = width; col > 0; col--) {
  53.       y = GETJSAMPLE(*inptr0++);
  54.       u = (int) GETJSAMPLE(*inptr1++) - CENTERJSAMPLE;
  55.       v = (int) GETJSAMPLE(*inptr2++) - CENTERJSAMPLE;
  56.       /* Note: if the inputs were computed directly from RGB values,
  57.        * range-limiting would be unnecessary here; but due to possible
  58.        * noise in the DCT/IDCT phase, we do need to apply range limits.
  59.        */
  60.       y *= 1024;    /* in case compiler can't spot common subexpression */
  61.       x = y          + 1436*v + 512; /* red */
  62.       if (x < 0) x = 0;
  63.       if (x > ((INT32) MAXJSAMPLE*1024)) x = (INT32) MAXJSAMPLE*1024;
  64.       *outptr0++ = (JSAMPLE) (x >> 10);
  65.       x = y -  352*u -  731*v + 512; /* green */
  66.       if (x < 0) x = 0;
  67.       if (x > ((INT32) MAXJSAMPLE*1024)) x = (INT32) MAXJSAMPLE*1024;
  68.       *outptr1++ = (JSAMPLE) (x >> 10);
  69.       x = y + 1815*u          + 512; /* blue */
  70.       if (x < 0) x = 0;
  71.       if (x > ((INT32) MAXJSAMPLE*1024)) x = (INT32) MAXJSAMPLE*1024;
  72.       *outptr2++ = (JSAMPLE) (x >> 10);
  73.     }
  74.   }
  75. }
  76.  
  77.  
  78. /*
  79.  * Color conversion for no colorspace change: just copy the data.
  80.  */
  81.  
  82. METHODDEF void
  83. null_convert (decompress_info_ptr cinfo, int num_rows,
  84.           JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  85. {
  86.   short ci;
  87.  
  88.   for (ci = 0; ci < cinfo->num_components; ci++) {
  89.     jcopy_sample_rows(input_data[ci], 0, output_data[ci], 0,
  90.               num_rows, cinfo->image_width);
  91.   }
  92. }
  93.  
  94.  
  95. /*
  96.  * Color conversion for grayscale: just copy the data.
  97.  * This also works for YCbCr/YIQ -> grayscale conversion, in which
  98.  * we just copy the Y (luminance) component and ignore chrominance.
  99.  */
  100.  
  101. METHODDEF void
  102. grayscale_convert (decompress_info_ptr cinfo, int num_rows,
  103.            JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  104. {
  105.   jcopy_sample_rows(input_data[0], 0, output_data[0], 0,
  106.             num_rows, cinfo->image_width);
  107. }
  108.  
  109.  
  110. /*
  111.  * Finish up at the end of the file.
  112.  */
  113.  
  114. METHODDEF void
  115. colorout_term (decompress_info_ptr cinfo)
  116. {
  117.   /* no work needed */
  118. }
  119.  
  120.  
  121. /*
  122.  * The method selection routine for output colorspace conversion.
  123.  */
  124.  
  125. GLOBAL void
  126. jseldcolor (decompress_info_ptr cinfo)
  127. {
  128.   /* Make sure num_components agrees with jpeg_color_space */
  129.   switch (cinfo->jpeg_color_space) {
  130.   case CS_GRAYSCALE:
  131.     if (cinfo->num_components != 1)
  132.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  133.     break;
  134.  
  135.   case CS_RGB:
  136.   case CS_YIQ:
  137.   case CS_YCbCr:
  138.     if (cinfo->num_components != 3)
  139.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  140.     break;
  141.  
  142.   case CS_CMYK:
  143.     if (cinfo->num_components != 4)
  144.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  145.     break;
  146.  
  147.   default:
  148.     ERREXIT(cinfo->emethods, "Unsupported JPEG colorspace");
  149.     break;
  150.   }
  151.  
  152.   /* Set color_out_comps and conversion method based on requested space */
  153.   switch (cinfo->out_color_space) {
  154.   case CS_GRAYSCALE:
  155.     cinfo->color_out_comps = 1;
  156.     if (cinfo->jpeg_color_space == CS_GRAYSCALE ||
  157.     cinfo->jpeg_color_space == CS_YCbCr ||
  158.     cinfo->jpeg_color_space == CS_YIQ)
  159.       cinfo->methods->color_convert = grayscale_convert;
  160.     else
  161.       ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  162.     break;
  163.  
  164.   case CS_RGB:
  165.     cinfo->color_out_comps = 3;
  166.     if (cinfo->jpeg_color_space == CS_YCbCr)
  167.       cinfo->methods->color_convert = ycc_rgb_convert;
  168.     else if (cinfo->jpeg_color_space == CS_RGB)
  169.       cinfo->methods->color_convert = null_convert;
  170.     else
  171.       ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  172.     break;
  173.  
  174.   case CS_CMYK:
  175.     cinfo->color_out_comps = 4;
  176.     if (cinfo->jpeg_color_space == CS_CMYK)
  177.       cinfo->methods->color_convert = null_convert;
  178.     else
  179.       ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  180.     break;
  181.  
  182.   default:
  183.     ERREXIT(cinfo->emethods, "Unsupported output colorspace");
  184.     break;
  185.   }
  186.  
  187.   if (cinfo->quantize_colors)
  188.     cinfo->final_out_comps = 1;    /* single colormapped output component */
  189.   else
  190.     cinfo->final_out_comps = cinfo->color_out_comps;
  191.  
  192.   cinfo->methods->colorout_init = colorout_init;
  193.   cinfo->methods->colorout_term = colorout_term;
  194. }
  195.